// GuiHandler.cs
//

namespace ServiceAdmin
{
	using ServiceAdmin.Config;
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Diagnostics;
    using System.Drawing;
    using System.Windows.Forms;


    /// <summary>
    /// Summary description for GuiHandler.
    /// </summary>
    public class GuiHandler : Form
    {
        private Monitor     	 _monitor         = null;
        private TaskIcon 	     _taskIcon        = null;
        private Button           _startButton     = new Button();
        private Button           _stopButton      = new Button();
        private Button           _resetButton     = new Button();

        private TabPage          _checkPage       = new TabPage();
        private TabPage          _settingPage     = new TabPage();

        private ContextMenu      _iconContextMenu = new ContextMenu();
        private MenuItem         _openItem        = null;
        private MenuItem         _quitItem        = null;
        private MenuItem         _startItem       = null;
        private MenuItem         _stopItem        = null;
        private MenuItem         _restartItem     = null;

		private PictureBox		 _pictureBox	  = new PictureBox();
		private StatusBar	     _statusBar		  = new StatusBar();
		private ServiceConfig	 _configReader    = null;

        public event EventHandler OnMinimize;
        public event EventHandler OnMaximize;
        public event EventHandler OnStart;
        public event EventHandler OnStop;
        public event EventHandler OnRestart;
        public event EventHandler OnQuit;
		public event EventHandler OnLanguageChange;


        // buttons
        public Button StartButton
        {
            get { return _startButton; }
        }


        public Button StopButton
        {
            get { return _stopButton; }
        }


        public Button ResetButton
        {
            get { return _resetButton; }
        }


        // tab pages
        public TabPage CheckPage
        {
            get { return _checkPage; }
        }


        public TabPage SettingsPage
        {
            get { return _settingPage; }
        }


        // task icon
        public TaskIcon TaskIcon
        {
            get { return _taskIcon; }
        }


        // menu items
        public MenuItem OpenItem
        {
            get { return _openItem; }
        }


        public MenuItem QuitItem
        {
            get { return _quitItem; }
        }


        public MenuItem StartItem
        {
            get { return _startItem; }
        }


        public MenuItem StopItem
        {
            get { return _stopItem; }
        }


        public MenuItem ResetItem
        {
            get { return _restartItem; }
        }


		// status picture box
		public PictureBox PictureBox
		{
			get { return _pictureBox; }
		}


		// status bars
		public StatusBar StatusBar
		{
			get { return _statusBar; }
		}



        /// <summary>
        /// Creates a new instance of GuiHandler
        /// </summary>
        public GuiHandler(string[] args)
        {
            InitComponent();

            _taskIcon             = new TaskIcon(OptionHandler.Instance.DefaultIcon, _iconContextMenu);
            _taskIcon.OnMaximize += new EventHandler(OnMaximizeClick);
            _monitor              = new Monitor(this);
            InitSize(args);
        }


        private void InitComponent()
        {
            Size          = new Size(300, 218);
            Location      = new Point(100, 100);
            MaximumSize   = Size;
            MinimumSize   = Size;
            AcceptButton  = _startButton;
            MaximizeBox   = false;
            ShowInTaskbar = false;
			SizeGripStyle = SizeGripStyle.Hide;
            Icon          = OptionHandler.Instance.DefaultIcon;

            InitButtons();
            InitTabControl();
            InitTabPages();
            InitIconContextMenu();
			InitStatusBar();
			InitPictureBox();

			_configReader    = new ServiceConfig(SettingsPage, new Point(20, 20), 30, OptionHandler.OPTION_PATH, "Language");
			_configReader.RestartMessage	= OptionHandler.Instance.GetLanguageRes("RestartMessage");
			_configReader.SaveMessage		= OptionHandler.Instance.GetLanguageRes("SaveMessage");
			_configReader.OnLanguageChange += new EventHandler(ChangeLanguage);
        }


		private void InitPictureBox()
		{
			_pictureBox.SizeMode	= PictureBoxSizeMode.AutoSize;
			_pictureBox.Visible		= true;
			_pictureBox.Location	= new Point(50, 60);
			_checkPage.Controls.Add(_pictureBox);
		}


		private void InitStatusBar()
		{
			_statusBar.SizingGrip = false;
			_statusBar.ShowPanels = false;
			Controls.Add(_statusBar);
		}


        private void InitTabPages()
        {
            _checkPage.Controls.AddRange(new Control[] {
                                        _startButton,
                                        _stopButton,
                                        _resetButton
                                     } );
        }


        private void InitTabControl()
        {
            TabControl tabContainer = new TabControl();
            tabContainer.Size = new Size(Size.Width - 10, 165);

            tabContainer.TabPages.AddRange(new TabPage[] {
                                    _checkPage,
                                    _settingPage
                                } );
            Controls.Add(tabContainer);
        }


        private void InitButtons()
        {
            _startButton.Size       = new Size(90, 20);
            _startButton.Location   = new Point(120, 35);
            _startButton.Enabled    = false;
            _startButton.Click     += new EventHandler(OnStartClick);

            _stopButton.Size        = new Size(90, 20);
            _stopButton.Location    = new Point(120, 65);
            _stopButton.Enabled     = false;
            _stopButton.Click      += new EventHandler(OnStopClick);

            _resetButton.Size       = new Size(90, 20);
            _resetButton.Location   = new Point(120, 95);
            _resetButton.Enabled    = false;
            _resetButton.Click     += new EventHandler(OnRestartClick);
        }


        private void InitIconContextMenu()
        {
            _openItem                = new MenuItem("open", new EventHandler(OnMaximizeClick));
            _openItem.Enabled        = true;
            _openItem.DefaultItem    = true;

            _quitItem                = new MenuItem("quit", new EventHandler(OnQuitClick));
            _quitItem.Enabled        = true;

            _startItem               = new MenuItem("start", new EventHandler(OnStartClick));
            _startItem.Enabled       = false;
            _startItem.BarBreak      = true;

            _stopItem                = new MenuItem("stop", new EventHandler(OnStopClick));
            _stopItem.Enabled        = false;

            _restartItem             = new MenuItem("restart", new EventHandler(OnRestartClick));
            _restartItem.Enabled     = false;

            _iconContextMenu.MenuItems.AddRange(new MenuItem[] {
                                                _openItem,
                                                _quitItem,
                                                _startItem,
                                                _stopItem,
                                                _restartItem
                                                } );
        }


        private void InitSize(string[] args)
        {
            string size = (args.Length > 0) ? args[0] : "";

            if (size == "-i")
            {
                OnMinimize(this, new EventArgs());
            }
        }


        private void OnStartClick(object sender, EventArgs e)
        {
            if (OnStart != null)
            {
                OnStart(sender, e);
            }
        }


        private void OnStopClick(object sender, EventArgs e)
        {
            if (OnStop != null)
            {
                OnStop(sender, e);
            }
        }


        private void OnRestartClick(object sender, EventArgs e)
        {
            if (OnRestart != null)
            {
                OnRestart(sender, e);
            }
        }


        private void OnQuitClick(object sender, EventArgs e)
        {
            if (OnQuit != null)
            {
                OnQuit(sender, e);
            }
        }


        private void OnMaximizeClick(object sender, EventArgs e)
        {
            if (OnMaximize != null)
            {
                OnMaximize(sender, e);
            }
        }


		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				for (int i = 0; i < Controls.Count; ++i)
				{
					Controls[i].Dispose();
				}
			}
			base.Dispose(disposing);
		}


		private void ChangeLanguage(object sender, EventArgs e)
		{
			if (OnLanguageChange != null)
			{
				OnLanguageChange(sender, e);
			}
		}


        protected override void OnClosing(CancelEventArgs e)
        {
            if (OnMinimize != null)
            {
                e.Cancel = true;
                OnMinimize(this, new EventArgs());
            }
        }
    }
}